package com.module.organization.manager;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.organization.dao.CreditDao;
import com.module.organization.dao.CreditImgDao;
import com.module.organization.dao.OrganizationDao;
import com.module.organization.vo.Credit;
import com.module.organization.vo.Creditimg;
import com.module.organization.vo.Organization;

public class CreditManager extends GenericHibernateManager<Credit, Long>{
	private static CreditManager manager;
	private static CreditImgDao credimgDao;
	 OrganizationDao orgDao=OrganizationDao.getInstance();
	@Override
	public GenericHibernateDao<Credit, Long> getDao() {
		return null;
	}
	
	public synchronized static CreditManager getInstance() {
		if (manager == null)
			manager = new CreditManager();
		return manager;
	}
	static Logger logger = Logger.getLogger(CreditManager.class.getName());
	
	CreditDao dao = CreditDao.getInstance();
	
	/**
	 * 保存企业
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public boolean  saveCredit(String id,List<String> list) throws Exception{
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		boolean isTrue = false;
		StringBuffer sb = new StringBuffer();
		sb.append("delete from Credit as obj where obj.id='"
				+ id + "'");
		try {
			dao.deleteByQuery(session,sb.toString());
			if(list.size()<=0){
				isTrue = true ;
			}

			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return isTrue;
	}
		
		/**
		 * <列表> 
		 * 
		 * @param bean
		 * @see [类、类#方法、类#成员]
		 */
		public Page list(Page pageObj,Credit pu) throws Exception{
			Session session = this.getSession();
			Page page = new Page();
			try {
				page = dao.getList(session,pu, pageObj.getStart(),pageObj.getLimit());
			} catch (Exception e) {
				throw e;
			} finally {
				this.closeSession();
			}
			return page;
		}

	
	/**
	 * 删除企业
	 * @param obj
	 * @throws Exception
	 */
	
	public boolean del(Credit obj) throws Exception{
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		boolean isTrue = false;
		try{
			String hql = "delete from Creditimg where creditId="+obj.getId();
			dao.delete(session, obj);
			dao.deleteByQuery(session, hql);
			tx.commit();
		}catch (Exception e){
			tx.rollback();
		}finally{
			this.closeSession();
		}
		return isTrue;
		
	}
	
	
	public Credit queryById(Long id) throws Exception {
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		try{
			Credit ce = dao.queryById(session, id);
			tx.commit();
			return ce;
		}catch (Exception e){
			tx.rollback();
		}finally{
			this.closeSession();
		}
		return null;
	}
	/**
	 * 判断org是否重复
	 * @param pname
	 * @return
	 * @throws Exception
	 */
	public boolean checkProid(String pname) throws Exception 
	{ 
	Session session =this.getSession(); 
	boolean bl = dao.getCreditqueryById( session, pname); 
	return bl; 
	}
	
	
	/**
	 * 保存，修改
	 * @param credit
	 * @return
	 * judge 用于保存判断信息
	 */
	public String updateOrsave(Credit credit){
		boolean isTrue = false;
		String judge = null;
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		try {
			credimgDao=CreditImgDao.getInstance();
			if(credit.getId()!=null&&credit.getId()>0){
				dao.update(session, credit);
				List<Creditimg> crimg = credit.getItems();
				String hql = "delete from Creditimg where creditId="+credit.getId();
				credimgDao.deleteByQuery(session, hql);
				for (Creditimg creditimg : crimg) {
					creditimg.setCreditId(credit.getId());
					credimgDao.save(session, creditimg);
				}
				judge = "4";
				if(judge!="4"){
					judge = "5";
				}
				tx.commit();
			}else{
				//判断用户名重复
					
				if(this.checkProid(credit.getOrgId())){
					judge = "1";
					 isTrue= false;
					 
				}else{
					dao.save(session,credit);
					List<Creditimg> crimg = credit.getItems();
					for (Creditimg creditimg : crimg) {
						creditimg.setCreditId(credit.getId());
						credimgDao.save(session, creditimg);
					}
					tx.commit();
					judge= "2";
					isTrue=true;
					
				}
				
			}
			//return isTrue;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		}finally{
			this.closeSession();
		}
		return judge;
	}

	public Credit queryByName(String name) {
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		try{
			Credit ce = dao.queryByName(session, name);
			tx.commit();
			return ce;
		}catch (Exception e){
			tx.rollback();
		}finally{
			this.closeSession();
		}
		return null;
	}



//	/**
//	 * 查询
//	 * @param orgType
//	 * @param orgId 协会Id
//	 * @return
//	 */
//	 public List getNameList(String orgType, String orgId){
//		    Session session = HibernateSessionFactory.getSession();
//	    	try{
//		      return orgDao.getIdNameList(session, orgType,orgId);
//		   }catch(Exception e){
//			  e.printStackTrace(); 
//		   }finally{
//			   this.closeSession();  
//		   }
//	    	return null;
//	 }
	 
	 /**
	  * 查询
	  */
	 public List getNameList(String orgId){
		 Session session = HibernateSessionFactory.getSession();
		 try{
			 return orgDao.getIdNameList(session, orgId);
		 }catch (Exception e) {
			 e.printStackTrace();
		 }finally{
			 this.closeSession();
		 }
		 return null;
	 }
	
}
